﻿using HuaCheng.DataAccess;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HuaCheng.Views
{
    public partial class ChlSelect : Form
    {
        public static bool firLine;//第一排是否全选
        public static bool secLine;//第二排是否全选
        //public delegate void WeekStepSet(List<int> ids,List<string> maps);//委托
        public delegate void WeekStepSet(Dictionary<int,string> chls);//委托
        public event WeekStepSet SelectedCopyChl;//事件
        public Dictionary<int, string> selectedCopyChls;//选中复制通道实际id + 通道映射号 如1-1
        public int mapLine;//每排通道个数
        public ChlSelect(string ip)
        {
            InitializeComponent();
            LoadChl(ip);//界面初始化
            selectedCopyChls = new Dictionary<int, string>();
        }
        /// <summary>
        /// 关闭时触发方法
        /// </summary>
        /// <param name="realIds"></param>
        /// <param name="maps"></param>
        public void OnClose(Dictionary<int,string> chls)//OnClose(List<int> realIds,List<string> maps)
        {
            if (SelectedCopyChl != null)
            {
                //SelectedCopyChl(realIds,maps);
                SelectedCopyChl(chls);
            }
        }
        /// <summary>
        /// 加载通道排布 并处理撤销的通道
        /// 1x8和1x16两种情况
        /// </summary>
        public void LoadChl(string ip)
        {
            List<int> rowAndcol = ChlMapHelper.QueryMapRowAndCol(ip);
            List<int> revoIds = ChlMapHelper.QueryRevokeChl(ip);
            mapLine = rowAndcol[1];//第二个元素为行数即柜数
            if (mapLine == 1)//1x16的情况
            {
                foreach (CheckBox chb in this.pnlSec.Controls)
                {
                    int realid = int.Parse(chb.Name.Substring(5));
                    chb.Text = (realid + 1).ToString();
                }
                btnSec.Text = "1";
            }
            if (revoIds.Count > 0)//如果有撤销的通道
            {
                foreach (int id in revoIds)
                {
                    foreach (CheckBox chbf in this.pnlFir.Controls)
                    {
                        int realid = GetRealId(chbf);
                        if (realid == id)
                        {
                            chbf.Enabled = false;
                            break;
                        }
                    }
                    foreach (CheckBox chbs in this.pnlSec.Controls)
                    {
                        int realid = GetRealId(chbs);
                        if (realid == id)
                        {
                            chbs.Enabled = false;
                            break;
                        }
                    }
                }
            }

        }
        /// <summary>
        /// 根据控件名称获取实际id
        /// </summary>
        /// <param name="chbf"></param>
        /// <returns></returns>
        private static int GetRealId(CheckBox chbf)
        {
            return int.Parse(chbf.Name.Substring(5));
        }
        /// <summary>
        /// 整柜选中1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFir_Click(object sender, EventArgs e)
        {
            firLine = !firLine;//取反
            SelectLine(1, firLine);
        }
        /// <summary>
        /// 整柜选中2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSec_Click(object sender, EventArgs e)
        {
            secLine = !secLine;
            SelectLine(2, secLine);
        }
        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            foreach (CheckBox chb in this.pnlFir.Controls)
            {
                if (chb.Checked)
                {
                    int realid = GetRealId(chb);
                    string map = btnFir.Text+"-"+chb.Text;
                    //selectedCopy.Add(realid);
                    //mapCopy.Add(map);
                    selectedCopyChls.Add(realid, map);

                }
            }
            foreach (CheckBox chb in this.pnlSec.Controls)
            {
                if (chb.Checked)
                {
                    int realid = GetRealId(chb);
                    string map = btnSec.Text + "-" + chb.Text;
                    selectedCopyChls.Add(realid, map);
                }
            }
            if (selectedCopyChls.Count == 0)
            {
                MessageBox.Show("没有选取任何通道！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //OnClose(selectedCopy,mapCopy);//把复制通道号传回复制工步界面
                OnClose(selectedCopyChls);
            }
            this.Close();
        }
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 为指定行全选或者取消全选
        /// </summary>
        /// <param name="line"></param>
        /// <param name="check"></param>
        public void SelectLine(int line,bool check)
        {
            if (line == 1)
            {
                foreach (CheckBox chb in this.pnlFir.Controls)
                {
                    if(chb.Enabled)
                        chb.Checked = check;
                }
            }
            if (line == 2)
            {
                foreach (CheckBox chb in this.pnlSec.Controls)
                {
                    if (chb.Enabled)
                        chb.Checked = check;
                }
            }
        }
    }
}
